//
//  RecommendViewController.m
//  Karaoke
//
//  Created by 蒋 浩锋 on 12-8-22.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendFoodView.h"
#import "UIImageView+WebCache.h"
#import "RoomInfoBarView.h"

@interface RecommendViewController ()
{
    __weak IBOutlet UIScrollView    *_scrollView;
    __gm_weak GMGridView            *_gmGridView;
    //    NSArray                         *tempFoodList;
    __weak IBOutlet FXLabel *lblPinMing;

    __weak IBOutlet FXLabel *lblDanWei;

    __weak IBOutlet FXLabel *lblDanJia;

    __weak IBOutlet FXLabel *lblYiXuan;
    int                             _index;
}

@end

@implementation RecommendViewController
@synthesize btnDianXuan;
@synthesize tempFoodList, delegate;
@synthesize popoverController;
@synthesize hFlowView;
@synthesize hPageControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    RoomInfoBarView *roomInfobar = [[RoomInfoBarView alloc] initWithXib];
    roomInfobar.frame = CGRectMake(10.0f, 19, roomInfobar.frame.size.width, roomInfobar.frame.size.height);
    [self.view addSubview:roomInfobar];
    
    
    hFlowView.delegate = self;
    hFlowView.dataSource = self;
    hFlowView.pageControl = hPageControl;
    hFlowView.minimumPageAlpha = 0.5;
    hFlowView.minimumPageScale = 0.9;
    lblPinMing.shadowColor = nil;
    lblPinMing.shadowOffset = CGSizeMake(0.0f, 2.0f);
    lblPinMing.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.75f];
    lblPinMing.shadowBlur = 5.0f;
    
    // Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self didScrollToPage:0 inFlowView:hFlowView];
}

- (void)viewDidUnload
{
    _scrollView = nil;
    lblPinMing = nil;
    lblDanWei = nil;
    lblDanJia = nil;
    lblYiXuan = nil;
    btnDianXuan = nil;
    [self setBtnDianXuan:nil];
    [self setHFlowView:nil];
    [self setHPageControl:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (IBAction)goBackAction:(id)sender
{
    if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(dismiss:)])
    {
        [self.delegate dismiss:self];
    }
}

- (void)socketDataHandleDidFoodList:(NSArray *)foodList
{
    tempFoodList = foodList;
    [_gmGridView reloadData];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    _index = floorf((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self updateInterface];
}
- (IBAction)btnDianXuanAction:(id)sender
{
    OKFoodObject *obj = [self.tempFoodList objectAtIndex:_index];
    CLog(@"FoodName : %@",obj.NameChs);
    [self performSegueWithIdentifier:@"recommendToInputView" sender:obj];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier]isEqualToString:@"recommendToInputView"])
    {
        PopViewController *viewController = segue.destinationViewController;
        viewController.delegate = self;
        viewController.tempFoodObject = sender;
    }
}
-(void)popViewControllerDidConfirm:(PopViewController *)popController
{
    [self.popoverController dismissPopoverAnimated:YES];
    [self updateInterface];
}

-(void)updateInterface
{
    OKFoodObject *obj = [self.tempFoodList objectAtIndex:_index];
    btnDianXuan.hidden = NO;
    lblPinMing.text = obj.NameChs;
    lblDanJia.text = obj.Price;
    lblDanWei.text = obj.Monad;
    lblYiXuan.text = @"0";
    for (OKFolioObject *folioObj in [SocketDataHandle sharedInstance].tmpSelectFood.foodSet)
    {
        if ([folioObj.FoodNo isEqualToString:obj.No])
        {
            lblYiXuan.text = [NSString stringWithFormat:@"%d",folioObj.Quantity];
            btnDianXuan.hidden = YES;
            break;
        }
    }
}
- (NSURL *)GetServerUrl:(NSString *)foodNO imgWidth:(CGFloat)width imgHeigth:(CGFloat)heigth
{
    NSString    *serverIP = [[NSUserDefaults standardUserDefaults] stringForKey:@"Server_Ip"];
    NSString    *url = [NSString stringWithFormat:@"http://%@/GetPhoto.aspx?Flag=00&id=%@&width=%f&height=%f", serverIP, foodNO, width, heigth];
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    return [NSURL URLWithString:url];
}


////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark PagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(PagedFlowView *)flowView;{
    return CGSizeMake(800, 550);
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(PagedFlowView *)flowView
{
    _index = pageNumber;
    [self updateInterface];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark PagedFlowView Datasource
//返回显示View的个数
- (NSInteger)numberOfPagesInFlowView:(PagedFlowView *)flowView{
    return [tempFoodList count];
}

//返回给某列使用的View
- (UIView *)flowView:(PagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index
{
    OKFoodObject        *tempFoodObj = [tempFoodList objectAtIndex:index];
    UIImageView *imageView = (UIImageView *)[flowView dequeueReusableCell];
    if (!imageView) {
        imageView = [[UIImageView alloc] init];
        imageView.layer.cornerRadius = 6;
        imageView.layer.masksToBounds = YES;
    }
    [imageView setImageWithURL:[self GetServerUrl:tempFoodObj.No imgWidth:1024 imgHeigth:768]];
    return imageView;
}

- (IBAction)pageControlValueDidChange:(id)sender {
    UIPageControl *pageControl = sender;
    [hFlowView scrollToPage:pageControl.currentPage];
    _index = pageControl.currentPage;
    [self updateInterface];
}
@end